package homework.day05;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author ThinkBook
 */
public class StudentManagement {
    private Student[] stus;
    //实际学生个数
    private int counter;
    //键盘录入对象
    private Scanner in;

    //无参构造器：默认容量100
    public StudentManagement() {
        stus = new Student[100];
        in = new Scanner(System.in);
    }

    //有参构造器：size表示初始容量
    public StudentManagement(int size) {
        stus = new Student[size];
        in = new Scanner(System.in);
    }

    //输出 操作提示
    public void prompt() {
        System.out.println("********************");
        System.out.println("*Student Management*");
        System.out.println("********************");
        System.out.println("* 1)Add");
        System.out.println("* 2)Delete");
        System.out.println("* 3)Update");
        System.out.println("* 4)Select");
        System.out.println("* 0)Quit");
        System.out.println("********************");
        System.out.print("Please choose your operate:");
    }

    //根据用户录入的学生名称，找到学生在数组中的索引返回
    private int findStudent() {
        int index = -1;
        String name;
        System.out.print("Please input name:");
//从键盘获取一个字符串
        name = in.next();
//请补全下面核心代码
//...
        for (int i = 0; i < counter; i++) {
            if (stus[i].getName().equals(name)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //下面就是增删改查 四个方法
    public void addStudent() {
        String name;
        int age;
        String gender;
        Student s;
//判断 管理系统容量是否 已满
        if (counter >= stus.length) {
            System.out.println("Add failure,too many student!");
            String string = Arrays.toString(stus);
            System.out.println("数组满后的数据" + string);
            // 扩容 数组拷贝，System.方法 Arrays.方法
            // 1. System.方法
//            Student[] b = new Student[stus.length*2];
//            System.arraycopy(stus,0,b,0,stus.length);
//            stus = b;
            // 2. Arrays.方法
            Student[] students = Arrays.copyOf(stus,stus.length*2);
            stus = students;
            return;
        }
        System.out.print("Please input name:");
        name = in.next();
        System.out.print("Please input age:");
        age = in.nextInt();
        System.out.print("Please input gender:");
        gender = in.next();
        s = new Student(name, age, gender);
//关键代码
        stus[counter] = s;
        counter++;
        System.out.println("Add student success!");
    }
    //请补全下面代码

    //删除
    public void deleteStudent() {
        int index = findStudent();

        if (index == -1) {
            System.out.println("Student not found.");
        } else {
            // 移动数组中的元素以删除已删除的学生
            for (int i = index; i < counter - 1; i++) {
                stus[i] = stus[i + 1];
            }
            stus[counter - 1] = null; // 清除最后一个元素
            counter--;
            System.out.println("Delete student success!");
        }
    }

    //更新
    public void updateStudent() {
        int index = findStudent();

        if (index == -1) {
            System.out.println("Student not found.");
        } else {
            System.out.print("Please input new age:");
            int newAge = in.nextInt();
            System.out.print("Please input new gender:");
            String newGender = in.next();

            // 更新
            stus[index].setAge(newAge);
            stus[index].setGender(newGender);

            System.out.println("Update student success!");
        }
    }


    public void selectStudent() {
        int index = findStudent();

        if (index == -1) {
            System.out.println("Student not found.");
        } else {//查询输出
            System.out.println("Student found:");
            System.out.println("Name: " + stus[index].getName());
            System.out.println("Age: " + stus[index].getAge());
            System.out.println("Gender: " + stus[index].getGender());
        }
    }




public static void main(String[] args){
//实例化对象
        StudentManagement sm = new StudentManagement(1);
//用户操作标志
        int option = -1;
        Loop: while(true){
// 打印提示信息
            sm.prompt();
//获取用户操作
            option = sm.in.nextInt();
            if(option < 0 || option > 4)
                continue;
            switch(option){//byte char short int
                case 1:
                    sm.addStudent();
                    break;
                case 2:
                    sm.deleteStudent();
                    break;
                case 3:
                    sm.updateStudent();
                    break;
                case 4:
                    sm.selectStudent();
                    break;
                case 0:
                    break Loop;
            }//end switch
        }// end while
        System.out.println("游戏结束, byebye");
    }//end main
}//end class

class Student {
    // 属性
    private String name;
    private int age;
    private String gender;
//默认|无参构造器
    public Student(){
    }
//有参构造器
    public Student(String name,int age,String gender){
        this.name=name;
        this.gender=gender;
        this.age = age;
    }
//get|set方法

    public String getName(){
        return name;
    }
    public void setAge(int age){
        this.age = age;
    }
    public int getAge(){
        return age;
    }
    public void setGender(String gender){
        this.gender = gender;
    }
    public String getGender(){
        return gender;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}
